package com.plugin.seed.web.support;

import com.plugin.seed.web.bind.annotation.LoginUser;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author Peter Fan
 */
public abstract class AbstractLoginUserMethodArgumentResolver implements HandlerMethodArgumentResolver {


	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.hasParameterAnnotation(LoginUser.class);
	}

	@Override
	public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
										NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
		LoginUser loginUser = parameter.getParameterAnnotation(LoginUser.class);
		return doResolve(loginUser, parameter, webRequest);
	}

	protected abstract Object doResolve(LoginUser loginUser, MethodParameter parameter, NativeWebRequest request);



}
